<template>
  <div class="container" :style="{ backgroundImage: bg }">
    <!--对话框 start-->
    <div>
      <!--添加-->
      <el-dialog
          draggable
          destroy-on-close
          v-model="dialogAddVisible"
          title="添加"
          width="70%"
          center
      >
        <el-form inline :model="addFormInfo" label-width="150px">
          <el-form-item label="评分（1 - 5分）">
            <el-rate v-model="addFormInfo.repairEvaluationScore"/>
          </el-form-item>
          <el-form-item label="评价内容">
            <el-input type="textarea" rows="6" style="width: 800px"
                      v-model="addFormInfo.repairEvaluationContent"></el-input>
          </el-form-item>
          <el-form-item label="学生" style="width: 300px">
            <el-select v-model="addFormInfo.studentId">
              <el-option v-for="item in userList.list" :key="item.userId"
                         :label="item.nickname"
                         :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogAddVisible = false">取消</el-button>
            <el-button type="primary" @click="sureHandler">确定</el-button>
          </span>
        </template>
      </el-dialog>
      <!--编辑-->
      <el-dialog
          draggable
          destroy-on-close
          v-model="dialogEditorVisible"
          title="编辑"
          width="70%"
          center
      >
        <el-form inline :model="editorFormInfo" label-width="150px">
          <el-form-item label="评分（1 - 5分）">
            <el-rate v-model="editorFormInfo.repairEvaluationScore"/>
          </el-form-item>
          <el-form-item label="评价内容">
            <el-input type="textarea" rows="6" style="width: 800px"
                      v-model="editorFormInfo.repairEvaluationContent"></el-input>
          </el-form-item>
          <el-form-item label="学生" style="width: 300px">
            <el-select v-model="editorFormInfo.studentId">
              <el-option v-for="item in userList.list" :key="item.userId"
                         :label="item.nickname"
                         :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogEditorVisible = false">取消</el-button>
            <el-button type="primary" @click="sureEditorHandler">确定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
    <!--对话框 end-->

    <!--顶部 start-->
    <div class="header" v-if="headerShow">
      <div class="search">
        <el-input
            class="input"
            size="large"
            @keyup.enter="searchHandle"
            v-model="searchInfo"
            placeholder="请输入关键字"
        ></el-input>
        <el-button icon="Search" size="large" @click="searchHandle" color="#3c8dbc">搜索</el-button>
      </div>
    </div>
    <!--顶部 end-->

    <!--表格 start-->
    <div class="table-container">
      <div class="table-header">
        <div class="button-list">
          <el-button icon="DocumentAdd" type="success" @click="addHander">新增</el-button>
        </div>
        <div class="icon-list">
          <el-button icon="search" color="#3c8dbc" @click="headerShow?headerShow=false:headerShow=true"></el-button>
          <el-button icon="RefreshRight" color="#269846" @click="reload"></el-button>
          <el-button icon="ArrowLeftBold" color="#133c9a" @click="onClickLeft"></el-button>
        </div>
      </div>
      <el-table class="table" :data="dataList.list" highlight-current-row height="400">
        <el-table-column label="评分（1 - 5分）" prop="repairEvaluationScore" align="center">
          <template #default="scope">
            <el-rate v-model="scope.row.repairEvaluationScore"/>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="评价内容" prop="repairEvaluationContent"
                         align="center"></el-table-column>
        <el-table-column show-overflow-tooltip label="评价时间" prop="repairEvaluationTime"
                         align="center"></el-table-column>
        <el-table-column show-overflow-tooltip label="学生" prop="studentId" align="center">
          <template #default="scope">
                  <span v-for="item in userList.list" :key="item.userId">
                      {{ scope.row.studentId == item.userId ? item.nickname : '' }}
                  </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="220">
          <template #default="scope">
            <el-button size="small" icon="edit" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button size="small" icon="delete" type="danger" @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          layout="prev,pager,next,jumper"
          :default-page-size="defaultPageSize"
          :total="totalData"
          @current-change="currentChangeHaddler"
      ></el-pagination>
    </div>
    <!--表格 end-->

    <!--底部 start-->
    <div class="bottom">
      <span>© 2025 YueMu Copyrigh</span>
    </div>
    <!--底部 end-->
  </div>
</template>

<script setup>
import repairAxios from "@/api/repair.js"
import {onMounted, reactive, ref} from "vue";
import {useRoute} from "vue-router"
import userAxios from "@/api/user.js";

const route = useRoute()

//顶部显示
const headerShow = ref(true);
/**
 * 返回
 */
const onClickLeft = () => {
  history.back();
};
/**
 * 刷新页面
 */
const reload = () => {
  location.reload();
};
//初始化分页查询数据
const dataList = reactive({
  list: [],
})
const userList = reactive({
  list: [],
});
//初始化总条数
const totalData = ref(0);
//当前页
const currentPage = ref(1);
//初始化分页显示条数
const defaultPageSize = ref(10);
//搜索初始化状态
const searchInfo = ref("");
//添加添加对话框控制器
const dialogAddVisible = ref(false);
//初始化添加对话框状态
const addFormInfo = reactive({
  repairEvaluationScore: "",
  repairEvaluationContent: "",
  repairEvaluationTime: "",
  repairReportId: route.params.name,
  studentId: "",
});
//编辑对话框控制器
const dialogEditorVisible = ref(false);
//初始化编辑对话框状态
const editorFormInfo = reactive({
  repairEvaluationId: "",
  repairEvaluationScore: "",
  repairEvaluationContent: "",
  repairEvaluationTime: "",
  repairReportId: "",
  studentId: "",
});
/**
 * 网路请求：分页查询
 *  */
const http = () => {
  repairAxios.repairEvaluation_searchByRepairReportId({
    repairReportId: route.params.name,
    search: searchInfo.value,
    page: currentPage.value,
    size: defaultPageSize.value,
  }).then((res) => {
    if (res.data.code === 200) {
      dataList.list = res.data.data.records;
      totalData.value = res.data.data.total;
    } else {
      ElMessage.error(res.data.message);
    }
  });
};
onMounted(() => {
  http()
  userAxios.user_findAll().then((res) => {
    if (res.data.code === 200) {
      userList.list = res.data.data;
    }
  })
});
/**
 * 分页
 */
const currentChangeHaddler = (nowPage) => {
  currentPage.value = nowPage;
  http();
};
/**
 * 搜索按钮
 */
const searchHandle = () => {
  http();
};
/**
 * 添加对话框弹出事件
 */
const addHander = () => {
  dialogAddVisible.value = true;
};
/**
 * 添加对话框 确定事件
 */
const sureHandler = () => {
  repairAxios.repairEvaluation_add({
    repairEvaluationScore: addFormInfo.repairEvaluationScore,
    repairEvaluationContent: addFormInfo.repairEvaluationContent,
    repairEvaluationTime: addFormInfo.repairEvaluationTime,
    repairReportId: addFormInfo.repairReportId,
    studentId: addFormInfo.studentId,
  }).then((res) => {
    if (res.data.code === 200) {
      dialogAddVisible.value = false;
      http();
    } else {
      ElMessage.error(res.data.message);
    }
  });
};
/**
 * 编辑对话框 弹出事件
 *  */
const handleEdit = (index, row) => {
  dialogEditorVisible.value = true;
  repairAxios.repairEvaluation_findById({
    repairEvaluationId: row.repairEvaluationId,
  }).then((res) => {
    if (res.data.code === 200) {
      editorFormInfo.repairEvaluationId = res.data.data.repairEvaluationId;
      editorFormInfo.repairEvaluationScore = res.data.data.repairEvaluationScore;
      editorFormInfo.repairEvaluationContent = res.data.data.repairEvaluationContent;
      editorFormInfo.repairEvaluationTime = res.data.data.repairEvaluationTime;
      editorFormInfo.repairReportId = res.data.data.repairReportId;
      editorFormInfo.studentId = res.data.data.studentId;
    } else {
      ElMessage.error(res.data.data.message);
    }
  });
};
/**
 * 编辑对话框 确定事件
 */
const sureEditorHandler = () => {
  repairAxios.repairEvaluation_update({
    repairEvaluationId: editorFormInfo.repairEvaluationId,
    repairEvaluationScore: editorFormInfo.repairEvaluationScore,
    repairEvaluationContent: editorFormInfo.repairEvaluationContent,
    repairEvaluationTime: editorFormInfo.repairEvaluationTime,
    repairReportId: editorFormInfo.repairReportId,
    studentId: editorFormInfo.studentId,
  }).then((res) => {
    if (res.data.code === 200) {
      dialogEditorVisible.value = false;
      http();
    } else {
      ElMessage.error(res.data.message);
    }
  });
};
/**删除 */
const handleDelete = (index, row) => {
  ElMessageBox.confirm("确定删除么", "删除", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    repairAxios.repairEvaluation_delete({
      repairEvaluationId: row.repairEvaluationId,
    }).then((res) => {
      if (res.data.code === 200) {
        ElMessage({
          type: "success",
          message: "删除成功！！！",
        });
        http();
      } else {
        ElMessage({
          type: "error",
          message: res.data.message,
        });
      }
    });
  }).catch((error) => {
    ElMessage({
      type: "info",
      message: "取消删除",
    });
  });
};
</script>
<style scoped lang="scss">
.container {
  padding-top: 20px;
  background-size: cover;
}

.header {
  background-color: #ffffff;
  margin-left: 10px;
  margin-right: 10px;
  margin-bottom: 15px;
  padding: 12px;
  border-radius: 10px;
  box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  display: flex;

  .search {
    width: 100%;

    .input {
      width: 45%;
    }
  }
}

.table-container {
  background-color: #ffffff;
  margin: 10px;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);

  .table-header {
    display: flex;

    .button-list {
      color: #ffffff;
    }

    .icon-list {
      position: fixed;
      right: 30px;
    }
  }

  .table {
    margin: 10px;
    width: 97%;
  }
}

.bottom {
  position: fixed;
  bottom: 0;
  height: 20px;
  background-color: #ffffff;
  width: 100%;
  padding: 10px;

  span {
    position: fixed;
    right: 20px;
    cursor: pointer;
  }
}
</style>
